
//向/rack/getBookList发送请求，返回一个data，data.books存有需要渲染的书籍，再渲染到bookrack中
function getRack(params,bookrack){
    /*这个函数取得一个存有书的id,书名，作者的字典，格式为{id:{name:name,author:author}}
        然后将书架渲染到传入的div中
        渲染格式为div#bookbox>(div#id>span+span)*n
        两个span分别存放书名和作者，div的id就是书的id
        再给这些元素添加onclick事件，点击打开一个layer用于展示这本书的详细信息*/
    console.log(params)
    $.ajax({
        //如果有筛选条件，则发送post请求
        url:"/rack/getBookList"
        ,type:params.type === 'filter' ? "post":"get"
        ,datatype:"JSON"
        ,data:racktype === 'filter' ? $('#filterform').serialize():{'params':params}
        ,success:function(result) {
            console.log(result);
            if (result.state == 1) {
                var books = result.data.books
                bookrack.empty()
                if (books.length !== 0) {
                    for (var j = 0, len = books.length; j < len; j++) {
                        bookrack.append(createBookEle(books[j]));
                    }
                } else {
                    makeRackEmpty(bookrack)
                }
            }
            else {
                popMessage(result.message);
            }
        }
    })}

//创建一本书
function createBookEle(book,rack){
    var ele = $("<div class='books card' id=" + book.id + "></div>")
    var name = $(buildHTML("p", book.name, {
        "class": "name-rack"
    }));
    var author = $(buildHTML("p", book.author, {
        "class": "author-rack"
    }));
    var cover = $(buildHTML("img", {
        "src": "/covers/" + book.cover,
        "class": "covers-rack card-img-top"
    }))
    ele.append(cover);
    ele.append(name);
    //需要判断这本书是否是当前用户的书，如果是，则显示未读消息数量
    //unread属性表示未读消息数量，
    if (book.unread > 0 && book.ownerId==statuses.loginedUserId) {
        var unread = $('<span class="badge badge-danger" style="position: absolute;top: 140px;right: 0px">' + book.unread + "未读</span>")
        name.append(unread)
    }
    ele.append(author);
    ele.click(
        (function (id) {
            var bookid = id;
            return function () {
                showBookLayer(bookid)
            }
        })(book.id));
    rack.append(ele);
}

//删除一本书
function deleteBookById(id) {
    $(".books").remove("#"+id);
}

//将已有的书架清空
function makeRackEmpty(rack) {
    rack.empty();
    var nulltext = $(buildHTML('p','暂无书籍',{
        'class':'nullnode'
    }))
    bookrack.append(nulltext);
}

//书籍详情弹出页面
function showBookLayer(id){
    parent.layer.open(
        {
            skin:'layui-layer-molv',
            type:2,
            content:"/booklayer/"+id+"/",
            area: ['500px', '610px'],
            title:false,
            scroll:false
        }
    )
}

function getShopRackHandle(){
    $("#personalNav").slideUp();
    $("#mainContent .w").empty().load("page/book/rack",function () {
        getRack({type:"shop"},$("#bookRack"))
    });
}
function getCityRackHandle(){
    $("#personalNav").slideUp();
    $("#mainContent .w").empty().load("page/book/rack",function () {
        getRack({type:"city"},$("#bookRack"))
    });
}
function getUserRackHandle(){
    $("#personalNav").slideUp();
    $("#mainContent .w").empty().load("page/book/rack",function () {
        getRack({type:"user"},$("#bookRack"))
    });
}
function getUserRackHandle(id){
    $("#personalNav").slideUp();
    $("#mainContent .w").empty().load("page/book/rack",function () {
        getRack({type:"other",id:id},$("#bookRack"))
    });
}
function InitRack() {
    getShopRackHandle();
}
